<?php

namespace app\admin\model;

use think\Model;

class Brand extends Model
{
    // protected $table = 'table_name';
    protected $pk = 'brand_id';
    public function searchKeywordsAttr($q, $v, $d)
    {
        $q->where('brand_name', 'like', "%{$v}%");
    }
    public function searchRecommendedAttr($q, $v, $d)
    {
        $q->where('recommended', '=', $v);
    }
    public function searchIfShowAttr($q, $v, $d)
    {
        $q->where('if_show', 'in', $v);
    }
    public function searchLogoarrAttr($q, $v, $d)
    {
        $q->where('brand_logo', '<>', "");
    }

    public static function getBrandList($where, $get, $whereOr)
    {
        $list = Brand::withSearch(array_keys($where), $where)
            ->where(function ($q) use ($whereOr) {
                $q->whereOr($whereOr);
            })
            ->order("sort_order")
            ->paginate([
                'list_rows' => 10,
                'var_page' => 'page',
                'query' => $get,
            ]);
        return $list;
    }
    public static function addBrand($data)
    {
        try {
            $res = Brand::create($data);
            $msg = '添加成功';
        } catch (\Exception $th) {
            $res = false;
            $msg = $th->getMessage();
        }
        return [$res, $msg];
    }
    public static function editBrand($data)
    {
        try {
            $res = Brand::update($data);
            $msg = '修改成功';
        } catch (\Exception $th) {
            $res = false;
            $msg = $th->getMessage();
        }
        return [$res, $msg];
    }
}
